package com.slf.flow.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

import com.slf.common.utils.Result;
import org.apache.commons.lang3.StringUtils;
import com.slf.flow.core.model.DataSource;
import com.slf.flow.core.service.DataSourceService;
import com.slf.flow.core.utils.DataSourceUtils;
import com.slf.flow.model.JsonBean;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/datasource")
public class DataSourceController {
	
	@Autowired
	private DataSourceService dataSourceService;
	
	@GetMapping("/list")
	public Result<IPage<DataSource>> list(@RequestParam(name = "page",defaultValue = "1")Integer page, @RequestParam(name = "limit",defaultValue = "1")Integer size) {
		Result<IPage<DataSource>> result = new Result<IPage<DataSource>>();

		return result.ok(dataSourceService.page(new Page<DataSource>(page, size), new QueryWrapper<DataSource>().select("id", "name", "driver_class_name", "create_date").orderByDesc("create_date")));
	}
	
	@GetMapping("/all")
	public Result<List<DataSource>> all(){
		Result<List<DataSource>> result = new Result<List<DataSource>>();
		return result.ok(dataSourceService.list());
	}
	
	@PostMapping("/save")
	public Result<String> save(@RequestBody DataSource dataSource){
		if(StringUtils.isNotBlank(dataSource.getId())){
			DataSourceUtils.remove(dataSource.getId());
		}
		Result<String> result = new Result<String>();
		dataSourceService.saveOrUpdate(dataSource);
		return result.ok(dataSource.getId());
	}
	@PutMapping("/save")
	public Result<String> update(@RequestBody DataSource dataSource){
		if(StringUtils.isNotBlank(dataSource.getId())){
			DataSourceUtils.remove(dataSource.getId());
		}
		Result<String> result = new Result<String>();
		dataSourceService.saveOrUpdate(dataSource);
		return result.ok(dataSource.getId());
	}
	
	@GetMapping("/get/{id}")
	public Result<DataSource> get(@PathVariable("id") String id){
		Result<DataSource> result = new Result<DataSource>();
		DataSource dataSource = dataSourceService.getById(id);
		dataSource.setPassword(null);
		return result.ok(dataSource);
	}
	
	@DeleteMapping("/delete/{id}")
	public Result<Boolean> remove(@PathVariable("id") String id){
		Result<Boolean> result =new Result<>();
		DataSourceUtils.remove(id);
		boolean b = dataSourceService.removeById(id);
		return result.ok(b);
	}
	
	@PostMapping("/test")
	public Result<String> test(@RequestBody DataSource dataSource){
		Result<String> result = new Result<>();
		if(StringUtils.isBlank(dataSource.getDriverClassName())){
			return result.error("DriverClassName不能为空！");
		}
		if(StringUtils.isBlank(dataSource.getJdbcUrl())){
			return result.error( "jdbcUrl不能为空！");
		}
		Connection connection = null;
		try {
			Class.forName(dataSource.getDriverClassName());
			String url = dataSource.getJdbcUrl();
			String username = dataSource.getUsername();
			String password = dataSource.getPassword();
			if(StringUtils.isNotBlank(username)){
				connection = DriverManager.getConnection(url,username,password);
			}else{
				connection = DriverManager.getConnection(url);
			}
			Result<String> res = result.ok("测试连接成功");
			res.setMsg("测试连接成功");
			return res;
		} catch (ClassNotFoundException e) {
			return result.error( "找不到驱动包：" + dataSource.getDriverClassName());
		} catch (Exception e){
			return result.error("连接失败，"+ e.getMessage());
		} finally{
			if(connection != null){
				try {
					connection.close();
				} catch (Exception e) {
				}
			}
		}
	}

}
